package com.statapp;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/** 
 * Main Activity containing a list of KeyIndicator Intems.
 * 
 * @author Peter Goldenbogen (Munich University of Applied Science)
 * @version 1.0 
 */  

public class StatAppActivity extends Activity {
	
	public static final String server_address = "chartservice.no-ip.org";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	
    	/** Send c2dm registration request in a extra thread */
		registerC2dm();
    	
    	/** Build list view */
    	String[] keyIndicators = new String [] {"Product Share","Sales Volume"};
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1, keyIndicators);
    	ListView listView = (ListView) findViewById(R.id.listView1);
    	listView.setAdapter(adapter);
   
    	listView.setOnItemClickListener(new OnItemClickListener() {
    		
    		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    			/** Selected Product Share */
    			if(position == 0){
    				ProductShare ps = new ProductShare();
    				Intent pieIntent = ps.getIntent(getApplicationContext());
    				startActivity(pieIntent);
    			}
    			/** Selected Sales Volume */
    			if(position == 1){
    				SalesVolume sv = new SalesVolume();
    				Intent lineIntent = sv.getIntent(getApplicationContext());
    				startActivity(lineIntent);
    			}
    		}
    	});
    }

    /**
     * Registering for C2DM
     */
	private void registerC2dm() {
		
		Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
    	registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
    	registrationIntent.putExtra("sender", "stat.application@googlemail.com");
    	startService(registrationIntent);
	}	 
}

